unit UnLog;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TFrmLog = class(TForm)
    mm: TMemo;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure log(aStr: string);
  end;

var
  FrmLog: TFrmLog;

implementation

uses
  uMinRu, UnMain;

{$R *.dfm}

{ TFrmLog }

procedure TFrmLog.FormCreate(Sender: TObject);
begin
  FrmLog := self;
end;

procedure TFrmLog.FormShow(Sender: TObject);
begin
  FrmMain.SetFormMonitor(FrmLog,FrmMain.screenNum);
end;

procedure TFrmLog.log(aStr: string);
var
  str,strFilePath: string;
  handlFile: Thandle;
  txtFile: Textfile;
begin
  try
    str := formatdatetime('yyyy-mm-dd hh:nn:ss.zzz',now()) + '    ' + aStr;
    mm.Lines.Add(AStr);
    SendMessage(mm.Handle, EM_SCROLLCARET, 0, 0);
    strFilePath := iMinRu.appPath + 'logs\';
    ForceDirectories(strFilePath);
    strFilePath := iMinRu.appPath+ 'logs\' +formatdatetime('yyyymmdd',now()) + 'log' + '.txt';
    if not fileexists(strFilePath) then
    begin
      handlFile := filecreate(strFilePath);
      FileClose(handlFile);
    end;
    assignfile(txtFile,strFilePath);
    append(txtFile);
    writeln(txtFile, str);
    closefile(txtFile);
  except
    exit;
  End;
end;

end.
